package com.skypan.myapplication.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.skypan.myapplication.LocalDataApplication;
import com.skypan.myapplication.R;
import com.skypan.myapplication.bean.LoginR;
import com.skypan.myapplication.bean.NoteR;
import com.skypan.myapplication.bean.RangeR;
import com.skypan.myapplication.http.NoteCallback;
import com.skypan.myapplication.http.RangeCallback;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;

import okhttp3.Call;
//生产企业操作人员——标签申请获批查询（查询页面）


public class TagRangeFragment extends BaseFragment{
    private String url = "http://39.105.165.31:8080/range/query";
    LoginR localuser = LocalDataApplication.getInstance().getLocalResponse();
    String companyId = localuser.data.companyId;
    private TagRangeFragment.MyAdapter adapter;
    private List<RangeR.ListBean> list ;
    @ViewInject(R.id.first_tag_range)
    private ListView firsttagrange;

    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tagrange,container,false);
    }

    @Override
    public void init() {
        rangequery(companyId);
    }


    public void rangequery(String companyid) {
        OkHttpUtils
                .get()
                .url(url)//
                .addParams("companyId", "1")
                .build()//
                .execute(new RangeCallback()
                {
                    @Override
                    public void onError(Call call, Exception e, int id)
                    {
                        Toast.makeText(getContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(RangeR response, int id)
                    {
//                        if(response.getCode()==0){
                        list =  response.data.list;
                        adapter = new TagRangeFragment.MyAdapter(getContext(),list);
                        firsttagrange.setAdapter(adapter);
//                        Toast.makeText(getContext(),list.get(0).tagId,Toast.LENGTH_SHORT).show();

//                        }

                    }
                });
    }

    public class MyAdapter extends BaseAdapter {

        private Context context;
        private  List<RangeR.ListBean> data;

        public MyAdapter(Context context, List<RangeR.ListBean> data){
            this.context = context;
            this.data = data;

        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder = new ViewHolder();

            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.range_list,parent,false);
                viewHolder.item_range_tagId = (TextView) convertView.findViewById(R.id.item_range_tagId);
                viewHolder.item_range_companyId = (TextView) convertView.findViewById(R.id.item_range_companyId);
                viewHolder.item_range_tagStatus = (TextView) convertView.findViewById(R.id.item_range_tagStatus);

                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.item_range_tagId.setText("标签范围："+data.get(position).getRangeId());
            viewHolder.item_range_companyId.setText("标签公司："+data.get(position).getCompanyId());
            if(data.get(position).getRangeStatus().equals("0"))
            viewHolder.item_range_tagStatus.setText("审核状态："+"通过");
            else if(data.get(position).getRangeStatus().equals("1")) viewHolder.item_range_tagStatus.setText("审核状态："+"审核中");
            else viewHolder.item_range_tagStatus.setText("审核状态："+"禁用");

            return convertView;
        }


        class ViewHolder {

            public TextView item_range_tagId;
            public TextView item_range_companyId;
            public TextView item_range_tagStatus;
        }


    }

}
